<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Wyp
 */

namespace App\Modules\Live\Repositories;


use App\Models\LiveLike;
use App\Modules\Live\Validators\LiveLikeValidator;
use Flower\Core\Repository\Eloquent\BaseRepository;
use App\Exceptions\AllValidatorException;

class LiveLikeRepository extends BaseRepository
{
    public function model()
    {
        return LiveLike::class;
    }

    public function validator()
    {
        return LiveLikeValidator::class;
    }

    /**
     * 点赞
     * @param $member_id
     * @param $live_id
     * @return mixed
     * @throws AllValidatorException
     */
    public function createLiveLike($member_id,$live_id)
    {
//        $liveLike = $this->getLiveLike($member_id,$live_id);
//        if($liveLike) {
//            throw new AllValidatorException(['msg' => '已点赞，请勿重复点赞']);
//        }
        $data = [
            'member_id' => $member_id,
            'live_id' => $live_id,
            'status' => 1,
        ];

        return $this->create($data);
    }

    /**
     * 取消点赞
     * @param $member_id
     * @param $live_id
     * @return mixed
     * @throws AllValidatorException
     */
    public function editLiveLike($member_id,$live_id)
    {
        $liveLike = $this->getLiveLike($member_id,$live_id);
        if(!$liveLike) {
            throw new AllValidatorException(['msg' => '尚未点赞']);
        }
        $data = [
            'status' => 0,
        ];
        return $this->update($data,$liveLike->id);
    }

    /**
     * 获取点赞信息
     * @param $member_id
     * @param $live_id
     * @return mixed
     */
    public function getLiveLike($member_id,$live_id)
    {
        $liveLike = $this->findWhere(['member_id'=>$member_id,'live_id'=>$live_id,'status'=>1],['id','status'])->first();
        return $liveLike;
    }

    /**
     * 获取点赞数
     * @param $live_id
     * @return mixed
     */
    public function getLiveLikeNum($live_id)
    {
        $liveLikeNum = $this->findWhere(['live_id'=>$live_id,'status'=>1])->count();
        return $liveLikeNum;
    }
}
